package com.iotproject.studyservice.controller.admin;

import com.iotproject.commonutils.R;
import com.iotproject.studyservice.entity.StudyListeningType;
import com.iotproject.studyservice.service.StudyListeningTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("admin/studyservice/studyListeningType/")
public class AdminStudyListeningTypeController {

    @Autowired
    StudyListeningTypeService studyListeningTypeService;

    @GetMapping("getListeningTypeList")
    public R getListeningTypeList() {
        List<StudyListeningType> list = studyListeningTypeService.list(null);
        return R.ok().data("listeningTypeList", list);
    }

    @DeleteMapping("deleteListeningTypeById/{id}")
    public R deleteListeningTypeById(@PathVariable("id") String id) {
        boolean removeById = studyListeningTypeService.removeById(id);
        if (removeById) {
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("getListeningType/{id}")
    public R getListeningType(@PathVariable("id") String id) {
        StudyListeningType studyListeningType = studyListeningTypeService.getById(id);
        return R.ok().data("studyListeningType", studyListeningType);
    }

    @PostMapping("updateListeningTypeMsg")
    public R updateListeningTypeMsg(@RequestBody StudyListeningType studyListeningType) {
        //studyListeningType 包含id
        boolean updateById = studyListeningTypeService.updateById(studyListeningType);
        if (updateById) {
            return R.ok();
        }
        return R.error();
    }

    @PostMapping("addListeningType")
    public R addListeningType(@RequestBody StudyListeningType studyListeningType) {
        boolean save = studyListeningTypeService.save(studyListeningType);
        if (save) {
            return R.ok();
        }
        return R.error();
    }

    //传入Excel文件，将内容存储到数据库
    @PostMapping("addListeningTypeByExcel")
    public R upload(MultipartFile file) {
        studyListeningTypeService.importStudyListeningTypeData(file, studyListeningTypeService);
        return R.ok();
    }

}
